package com.szxy.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szxy.item.service.ItemParamService;
import com.szxy.mapper.TbItemParamMapper;
import com.szxy.pojo.TbItemParam;
import com.szxy.pojo.TbItemParamExample;
import com.szxy.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/7 16:36
 * @Description:com.szxy.item.service.impl
 * @Version:1.0
 **/
@Service
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper itemParamMapper;

    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        //查询大字段使用带有 BLOBS
        List<TbItemParam> itemParamList = this.itemParamMapper.selectByExampleWithBLOBs(example);
        if(itemParamList !=null && itemParamList.size() >0){
            return itemParamList.get(0);
        }
        return null;
    }

    @Override
    public Integer insertTbItemParam(TbItemParam itemParam) {
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        return this.itemParamMapper.insert(itemParam);
    }

    @Override
    public PageResult selectItemParamAll(Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        TbItemParamExample example = new TbItemParamExample();
        List<TbItemParam> itemParamList = this.itemParamMapper.selectByExampleWithBLOBs(example);
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(itemParamList);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(pageInfo.getList());
        return pageResult;
    }

    @Override
    public int deleteItemParamById(Long id) {
        return this.itemParamMapper.deleteByPrimaryKey(id);
    }

}
